package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.CoursePublishClient;
import com.xuecheng.learning.mapper.ChooseCourseMapper;
import com.xuecheng.learning.model.dto.ChooseCourseDto;
import com.xuecheng.learning.model.dto.CourseTablesDto;
import com.xuecheng.learning.model.po.ChooseCourse;
import com.xuecheng.learning.model.po.CourseTables;
import com.xuecheng.learning.service.ChooseCourseService;
import com.xuecheng.learning.service.CourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class ChooseCourseServiceImpl extends ServiceImpl<ChooseCourseMapper,
        ChooseCourse> implements ChooseCourseService {
    @Autowired
    CoursePublishClient coursePublishClient;

    @Autowired
    CourseTablesService courseTablesService;

    /**
     * 学成选课
     * @param courseId
     * @param userId
     * @return
     */
    @Override
    public ChooseCourseDto chooseCourse(Long courseId, Long userId) {
        //首先查询当前课程信息
        CoursePublish coursePublish = coursePublishClient.getCoursePublish(courseId);
        //然后判断当前课程的收费类型
        String charge = coursePublish.getCharge();
        ChooseCourse chooseCourse = null;
        if("201000".equals(charge)){
            //免费课程
            //添加免费课程到选课表
            chooseCourse = addFreeCourse(coursePublish,userId);
            //添加免费课程到我的课程表
            CourseTables courseTables = addCourseTables(chooseCourse);
        }else{
            //添加收费课程
            chooseCourse = addChargeCourse(coursePublish,userId);
        }
        //获取学习资格
        ChooseCourseDto dto = new ChooseCourseDto();
        BeanUtils.copyProperties(chooseCourse,dto);
        CourseTablesDto tablesDto = courseTablesService.getLearnStatus(userId,courseId);
        dto.setLearnStatus(tablesDto.getLearnStatus());
        return dto;
    }

    /**
     * 添加收费课程
     * @param publish
     * @param userId
     * @return
     */
    private ChooseCourse addChargeCourse(CoursePublish publish, Long userId) {
        ChooseCourse choose = getOne(new LambdaQueryWrapper<ChooseCourse>()
                .eq(ChooseCourse::getCourseId,publish.getId())
                .eq(ChooseCourse::getUserId,userId));
        if(choose != null)  return choose;
        choose = new ChooseCourse();
        choose.setCourseId(publish.getId());
        choose.setCourseName(publish.getName());
        choose.setCoursePrice(publish.getPrice());
        choose.setUserId(userId);
        choose.setCompanyId(publish.getCompanyId());
        choose.setOrderType("700002");
        choose.setValidDays(365);
        choose.setStatus("701002");
        choose.setValidtimeStart(LocalDateTime.now());
        choose.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        save(choose);
        return choose;
    }

    /**
     * 添加课程到我的课程表
     * @param chooseCourse
     * @return
     */
    public CourseTables addCourseTables(ChooseCourse chooseCourse) {
        if(!"701001".equals(chooseCourse.getStatus())){
            XueChengPlusException.cast("当前课程未选课成功，无法添加到课程表");
        }

        CourseTables tables = courseTablesService.getOne(
                new LambdaQueryWrapper<CourseTables>()
                        .eq(CourseTables::getCourseId,chooseCourse.getCourseId())
                        .eq(CourseTables::getUserId,chooseCourse.getUserId()));
        if(tables != null)  return tables;
        tables = new CourseTables();
        BeanUtils.copyProperties(chooseCourse,tables);
        tables.setChooseCourseId(chooseCourse.getId());
        tables.setId(null);
        courseTablesService.save(tables);
        return tables;
    }

    /**
     * 添加免费课程
     * @param publish
     * @param userId
     * @return
     */
    private ChooseCourse addFreeCourse(CoursePublish publish, Long userId) {
        ChooseCourse choose = getOne(new LambdaQueryWrapper<ChooseCourse>()
                .eq(ChooseCourse::getCourseId,publish.getId())
                .eq(ChooseCourse::getUserId,userId)
                .eq(ChooseCourse::getOrderType,"700001")
                .eq(ChooseCourse::getStatus,"701001"));
        if(choose != null)  return choose;
        choose = new ChooseCourse();
        choose.setCourseId(publish.getId());
        choose.setCourseName(publish.getName());
        choose.setCoursePrice(0.0);
        choose.setUserId(userId);
        choose.setCompanyId(publish.getCompanyId());
        choose.setOrderType("700001");
        choose.setValidDays(365);
        choose.setStatus("701001");
        choose.setValidtimeStart(LocalDateTime.now());
        choose.setValidtimeEnd(LocalDateTime.now().plusDays(365));
        save(choose);
        return choose;
    }
}
